import org.apache.commons.lang3.StringUtils;
import util.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Application {

    public static void main(String[] args) throws Exception {
        File[] list = FileUtils.readLocationFolder("E:\\JavaProject\\xmn-debit-card\\xmn-debit-card-interface\\src\\main\\java\\com\\weimob\\xmn\\debit\\card\\model\\accountrecord");
        dealFolder(list);
    }

    public static void dealFolder(File[] list) throws IOException {
        if(list == null) {
            return;
        }
        for(File file : list) {
            if(file.isFile()) {
                dealModel(file.getAbsolutePath());
            } else {
                dealFolder(file.listFiles());
            }
        }
    }

    public static void dealService(String filepath) throws IOException {
        BufferedReader br = FileUtils.readLocationFile(filepath);
        StringBuilder sb = new StringBuilder();
        String line = null;
        String desc = null;
        while ((line = br.readLine()) != null) {
            if(line.contains(" * ")) {
                desc = line.replaceAll("\\*", "").replaceAll("\\n", "").trim();
            }
            if(line.contains("HttpResponse<")) {
                Pattern pattern = Pattern.compile("(?<=HttpResponse<).*(?=,)");
                Matcher matcher = pattern.matcher(line);
                if(matcher.find()) {
                    String response = matcher.group().trim();
                    String[] arr = response.split("<");
                    response = arr[0];
                    sb.append("\t").append("@ApiOperation(value = \"").append(desc).append("\", response = ").append(response).append(".class)\n");
                }
            }
            sb.append(line).append("\n");
        }
        // 复写
        FileUtils.overWriteLocationFile(filepath, sb.toString());
    }

    public static void dealModel(String filepath) throws IOException {
        BufferedReader br = FileUtils.readLocationFile(filepath);
        StringBuilder sb = new StringBuilder();
        String line = null;
        String desc = null;
        while ((line = br.readLine()) != null) {
            if(line.contains(" * ") && !line.contains("@")) {
                desc = line.replaceAll("\\*", "").replaceAll("\\n", "").trim();
            }
            if(line.contains("private") && StringUtils.isNotBlank(desc)) {
                sb.append("\t").append("@ApiModelProperty(\"").append(desc).append("\")\n");
            }
            sb.append(line).append("\n");
            if(line.contains("package")) {
                sb.append("\n").append("import io.swagger.annotations.ApiModelProperty;").append("\n");
            }
        }
        // 复写
        FileUtils.overWriteLocationFile(filepath, sb.toString());
    }

}
